package com.third.customer.model.dao.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.third.customer.model.dao.CustomerDao;
import com.third.customer.model.dao.param.CustomerParam;
import com.third.customer.model.mapper.CustomerMapper;
import com.third.customer.model.tables.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * Created by hehuaichun on 2020/5/21.
 */
@Repository
public class CustomerDaoImpl implements CustomerDao {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Page<Customer> query(CustomerParam param) {
        Page<Customer> page = new Page<>(param.getPage(), param.getLimit());
        page.addOrder(OrderItem.desc("id"));
        return new LambdaQueryChainWrapper<>(customerMapper)
                .eq(Customer::getCompanyId, param.getCompanyId())
                .eq(param.getAreaId() != null, Customer::getAreaId, param.getAreaId())
                .eq(param.getGroupId() != null, Customer::getGroupId, param.getGroupId())
                .like(!StringUtils.isEmpty(param.getName()), Customer::getName, param.getName())
                .like(!StringUtils.isEmpty(param.getPhone()), Customer::getPhone, param.getPhone())
                .page(page);
    }

    @Override
    public Optional<Customer> getById(long companyId, Long customerId) {
        return Optional.ofNullable(new LambdaQueryChainWrapper<>(customerMapper)
                .eq(Customer::getCompanyId, companyId)
                .eq(Customer::getId, customerId).one());
    }

    @Override
    public List<Customer> getByAreaId(long companyId, long areaId) {
        return new LambdaQueryChainWrapper<>(customerMapper)
                .eq(Customer::getCompanyId, companyId)
                .eq(Customer::getAreaId, areaId)
                .list();
    }

    @Override
    public List<Customer> getByGroupId(long companyId, long groupId) {
        return new LambdaQueryChainWrapper<>(customerMapper)
                .eq(Customer::getCompanyId, companyId)
                .eq(Customer::getGroupId, groupId)
                .list();
    }

    @Override
    public Optional<Customer> getByPhone(Long companyId, String phone) {
        return Optional.ofNullable(new LambdaQueryChainWrapper<>(customerMapper)
                .eq(Customer::getCompanyId, companyId)
                .eq(Customer::getPhone, phone).one());
    }

    @Override
    public List<Customer> getByCompanyId(Long companyId) {
        return new LambdaQueryChainWrapper<>(customerMapper)
                .eq(Customer::getCompanyId, companyId)
                .list();
    }

    @Override
    public boolean insert(Customer customer) {
        return customerMapper.insert(customer) == 1;
    }

    @Override
    public boolean update(Customer customer) {
        return customerMapper.updateById(customer) == 1;
    }

    @Override
    public boolean delete(Long id) {
        return customerMapper.deleteById(id) == 1;
    }
}
